//
//  MVCVC.m
//  Demo_MVC
//
//  Created by 陈冰 on 2020/3/8.
//  Copyright © 2020 ChenBing. All rights reserved.
//

#import "MVCVC.h"
#import "MVCTableViewCell.h"
#import "MVCModel.h"

static NSString *const cellRI = @"CellReuseIdentifier";
@interface MVCVC () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray <MVCModel *> *dataArray;

@end

@implementation MVCVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = [NSMutableArray new];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MVCTableViewCell class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:cellRI];
    // 异步加载数据
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 加载数据
        [self loadData];
        sleep(2);
        dispatch_async(dispatch_get_main_queue(), ^{
            // 刷新
            [self.tableView reloadData];
        });
    });
}

- (void)loadData{
    NSArray *tempArray =
    @[
        @{@"name":@"CadsC",@"imageUrl":@"http://CC",@"num":@"9"},
        @{@"name":@"Jadsfes",@"imageUrl":@"http://James",@"num":@"9"},
        @{@"name":@"Gadsfin",@"imageUrl":@"http://Gavin",@"num":@"9"},
        @{@"name":@"adci",@"imageUrl":@"http://Cooci",@"num":@"9"},
        @{@"name":@"adn",@"imageUrl":@"http://Dean ",@"num":@"9"},
        @{@"name":@"sd",@"imageUrl":@"http://CC",@"num":@"9"},
        @{@"name":@"Jadfes",@"imageUrl":@"http://James",@"num":@"9"},
        @{@"name":@"adin",@"imageUrl":@"http://Gavin",@"num":@"9"},
        @{@"name":@"adfci",@"imageUrl":@"http://Cooci",@"num":@"9"},
        @{@"name":@"CadsC",@"imageUrl":@"http://CC",@"num":@"9"},
        @{@"name":@"Jadsfes",@"imageUrl":@"http://James",@"num":@"9"},
        @{@"name":@"Gadsfin",@"imageUrl":@"http://Gavin",@"num":@"9"},
        @{@"name":@"adci",@"imageUrl":@"http://Cooci",@"num":@"9"},
        @{@"name":@"CadsC",@"imageUrl":@"http://CC",@"num":@"9"},
        @{@"name":@"Jadsfes",@"imageUrl":@"http://James",@"num":@"9"},
        @{@"name":@"Gadsfin",@"imageUrl":@"http://Gavin",@"num":@"9"},
        @{@"name":@"adci",@"imageUrl":@"http://Cooci",@"num":@"9"},
        @{@"name":@"Dsdfn",@"imageUrl":@"http://Dean ",@"num":@"9"}
    ];
    for (NSInteger i = 0; i< tempArray.count; i++) {
        MVCModel *m = [MVCModel modelWithDict:tempArray[i]];
        [self.dataArray addObject:m];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MVCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellRI forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

/*改变删除按钮的title*/
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle  forRowAtIndexPath:(NSIndexPath *)indexPath {
    if  (editingStyle  ==  UITableViewCellEditingStyleDelete) {
        /*先删除数据，再执行UI*/
        // 一维数组删除某行
        [self.dataArray  removeObjectAtIndex:indexPath.row];
        // 二维数组，删除某个section下的行
        // [[self.dataArray objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];
        /* 删除tableView中的一行，动画自动，单行刷新*/
        [tableView  deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath]  withRowAnimation:UITableViewRowAnimationAutomatic];
        //报错则用 [self.tableView reloadData];
    }
}

@end

